package com.itheima.tlias84.controller;

import com.aliyun.core.annotation.Path;
import com.itheima.tlias84.dto.SelectAllClazzDTO;
import com.itheima.tlias84.dto.SelectClazzDetailDTO;
import com.itheima.tlias84.mapper.ClazzMapper;
import com.itheima.tlias84.po.InsertClazzParam;
import com.itheima.tlias84.po.SelectClazzPageParam;
import com.itheima.tlias84.po.UpdateClazzParam;
import com.itheima.tlias84.service.ClazzService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Resource
    ClazzService clazzService;
    @Autowired
    private ClazzMapper clazzMapper;

    //分页
    @GetMapping
    ResultUtil selectClazzByPage(  SelectClazzPageParam param){
        if(Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }
        return clazzService.selectClazzByPage(param);
    }
    //更新
    @PutMapping
    public ResultUtil updateClazz(@RequestBody UpdateClazzParam param){
        int flag = clazzMapper.updateClazz(param);
        return flag == 0? ResultUtil.fail():ResultUtil.success();
    }
    //根据id查询
    @GetMapping("/{id}")
    public  ResultUtil selectClazzById(@PathVariable("id") Integer id){
        SelectClazzDetailDTO dto = clazzService.selectClazzById(id);
        return ResultUtil.success(dto);
    }
    //根据id删除
    @DeleteMapping("/{id}")
    public ResultUtil deleteClazzById(@PathVariable("id") Integer id){
        int flag = clazzService.deleteClazzById(id);
        return flag == 0 ? ResultUtil.fail():ResultUtil.success();
    }
    //新增班级信息
    //post请求要从求情体中取
    @PostMapping
    public ResultUtil insertClazz(@RequestBody InsertClazzParam param){
        int flag = clazzService.insertClazz(param);
        return flag == 0 ? ResultUtil.fail():ResultUtil.success();
    }
    //查询所有班级
    @GetMapping("/list")
   public ResultUtil  selectAllClazz(){
        List<SelectAllClazzDTO> dtos = clazzService.selectAllClazz();
        return ResultUtil.success(dtos);
    }
}
